package com.ssm.web.controller;

import com.ssm.entity.Dept;
import com.ssm.entity.PageResult;
import com.ssm.entity.QueryObject;
import com.ssm.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/department")
public class DeptController {
    @Autowired
    private DeptService deptService;

    //查看localhost:8088/department/list
    @RequestMapping("/list")
    public String list(Model model, QueryObject query) {
        PageResult<Dept> pageResult = deptService.query(query);
        model.addAttribute("pageResult", pageResult);
        return "department/list";
    }

    //删除localhost:8088/department/delete?id=xxx
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
            deptService.delete(id);
        }
        return "redirect:/department/list";//返回上面的查询所有list方法
    }

    //新增localhost:8088/department/input
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            Dept dept = deptService.sqlById(id);
            model.addAttribute("dept", dept);
        }
        return "department/input";
    }

    //处理localhost:8088/department/saveOrUpdate
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Dept dept) {
        if (dept.getId() != null) {
            deptService.update(dept);
        } else {
            deptService.save(dept);
        }
        return "redirect:/department/list";
    }
}
